package com.kkd.kkdapp.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.utils.SystemUtils;

/**
 * 日历控件
 */
@SuppressWarnings("deprecation")
public class KCalendar extends ViewFlipper implements
		android.view.GestureDetector.OnGestureListener {
	public static final int COLOR_BG_WEEK_TITLE = Color.parseColor("#ffffffff"); // 星期标题背景颜色
	public static final int COLOR_TX_WEEK_TITLE = Color.parseColor("#999999"); // 星期标题文字颜色
	public static final int COLOR_TX_THIS_MONTH_DAY = Color
			.parseColor("#aa564b4b"); // 当前月日历数字颜色
	public static final int COLOR_TX_OTHER_MONTH_DAY = Color
			.parseColor("#ffcccccc"); // 其他月日历数字颜色
	public static final int COLOR_TX_THIS_DAY = Color.parseColor("#ff008000"); // 当天日历数字颜色
	public static final int COLOR_BG_THIS_DAY = Color.parseColor("#ffcccccc"); // 当天日历背景颜色
	public static final int COLOR_BG_CALENDAR = Color.parseColor("#ffffffff"); // 日历背景色

	/**
	 * 只展示红色圆点
	 */
	public static final int POINT_RED = 10001;
	/**
	 * 只展示蓝色圆点
	 */
	public static final int POINT_BLUE = 10002;
	/**
	 * 展示红色和蓝色圆点
	 */
	public static final int POINT_RED_BLUE = 10003;

	private GestureDetector gd; // 手势监听器
	private Animation push_left_in; // 动画-左进
	private Animation push_left_out; // 动画-左出
	private Animation push_right_in; // 动画-右进
	private Animation push_right_out; // 动画-右出

	private int ROWS_TOTAL = 6; // 日历的行数
	private int COLS_TOTAL = 7; // 日历的列数
	private String[][] dates = new String[6][7]; // 当前日历日期
	private float tb;

	private OnCalendarClickListener onCalendarClickListener; // 日历翻页回调
	private OnCalendarDateChangedListener onCalendarDateChangedListener; // 日历点击回调

	private String[] weekday = new String[] { "日", "一", "二", "三", "四", "五", "六" }; // 星期标题

	private int calendarYear; // 日历年份
	private int calendarMonth; // 日历月份
	private Date thisday = new Date(); // 今天
	private Date calendarday; // 日历这个月第一天(1号)

	private LinearLayout firstCalendar; // 第一个日历
	private LinearLayout secondCalendar; // 第二个日历
	private LinearLayout currentCalendar; // 当前显示的日历

	private Map<String, Integer> marksMap = new HashMap<String, Integer>(); // 储存某个日子被标注(Integer
																			// 为bitmap
																			// res
																			// id)
	private Map<String, Integer> dayBgColorMap = new HashMap<String, Integer>(); // 储存某个日子的背景色

	private List<Date> dateList = new ArrayList<Date>();
	private boolean isOnScroll = false;

	public KCalendar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		dateList.add(new Date(2016 - 1990, 1, 1));
		dateList.add(new Date(2016 - 1990, 3, 1));
		dateList.add(new Date(2016 - 1990, 6, 1));
		dateList.add(new Date(2016 - 1990, 7, 1));
		dateList.add(new Date(2016 - 1990, 11, 1));
	}

	public KCalendar(Context context) {
		super(context);
		init();
	}

	private void init() {
		setBackgroundColor(COLOR_BG_CALENDAR);
		// 实例化收拾监听器
		gd = new GestureDetector(this);
		// 初始化日历翻动动画
		push_left_in = AnimationUtils.loadAnimation(getContext(),
				R.anim.push_left_in);
		push_left_out = AnimationUtils.loadAnimation(getContext(),
				R.anim.push_left_out);
		push_right_in = AnimationUtils.loadAnimation(getContext(),
				R.anim.push_right_in);
		push_right_out = AnimationUtils.loadAnimation(getContext(),
				R.anim.push_right_out);
		push_left_in.setDuration(400);
		push_left_out.setDuration(400);
		push_right_in.setDuration(400);
		push_right_out.setDuration(400);
		// 初始化第一个日历
		firstCalendar = new LinearLayout(getContext());
		firstCalendar.setOrientation(LinearLayout.VERTICAL);
		firstCalendar.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
		// 初始化第二个日历
		secondCalendar = new LinearLayout(getContext());
		secondCalendar.setOrientation(LinearLayout.VERTICAL);
		secondCalendar.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
		// 设置默认日历为第一个日历
		currentCalendar = firstCalendar;
		// 加入ViewFlipper
		addView(firstCalendar);
		addView(secondCalendar);
		// 绘制线条框架
		drawFrame(firstCalendar);
		drawFrame(secondCalendar);
		// 设置日历上的日子(1号)
		calendarYear = thisday.getYear() + 1900;
		calendarMonth = thisday.getMonth();
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		// 填充展示日历
		setCalendarDate();
	}

	private void drawFrame(LinearLayout oneCalendar) {

		LinearLayout weekcontainer = new LinearLayout(getContext());
		weekcontainer.setOrientation(LinearLayout.VERTICAL);
		weekcontainer
				.setLayoutParams(new LinearLayout.LayoutParams(-1, 0, 0.7f));

		// 添加周末线性布局
		LinearLayout title = new LinearLayout(getContext());
		title.setBackgroundColor(COLOR_BG_WEEK_TITLE);
		title.setOrientation(LinearLayout.HORIZONTAL);
		LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(-1, 0,
				1f);
		Resources res = getResources();
		tb = SystemUtils.dip2px(getContext(), 10);
		title.setLayoutParams(layout);
		weekcontainer.addView(title);

		// 添加周末TextView
		for (int i = 0; i < COLS_TOTAL; i++) {
			TextView view = new TextView(getContext());
			view.setGravity(Gravity.CENTER);
			view.setText(weekday[i]);
			view.setTextColor(COLOR_TX_WEEK_TITLE);
			view.setLayoutParams(new LinearLayout.LayoutParams(0, -1, 1));
			title.addView(view);
		}
		// 分割线
		LinearLayout line = new LinearLayout(getContext());
		line.setOrientation(LinearLayout.VERTICAL);
		line.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, SystemUtils.dip2px(
						getContext(), 0.5f)));
		line.setBackgroundColor(Color.parseColor("#e5e5e5"));
		weekcontainer.addView(line);

		oneCalendar.addView(weekcontainer);
		// 添加日期布局
		LinearLayout content = new LinearLayout(getContext());
		content.setOrientation(LinearLayout.VERTICAL);
		content.setLayoutParams(new LinearLayout.LayoutParams(-1, 0, 6f));
		oneCalendar.addView(content);
		// 添加日期TextView
		for (int i = 0; i < ROWS_TOTAL; i++) {
			LinearLayout row = new LinearLayout(getContext());
			row.setOrientation(LinearLayout.HORIZONTAL);
			row.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, 0, 1));
			content.addView(row);
			// 绘制日历上的列
			for (int j = 0; j < COLS_TOTAL; j++) {
				RelativeLayout col = new RelativeLayout(getContext());
				col.setLayoutParams(new LinearLayout.LayoutParams(0,
						LayoutParams.MATCH_PARENT, 1));
				col.setPadding(SystemUtils.dip2px(getContext(), 2),
						SystemUtils.dip2px(getContext(), 2),
						SystemUtils.dip2px(getContext(), 2),
						SystemUtils.dip2px(getContext(), 2));
				col.setBackgroundColor(Color.WHITE);
				row.addView(col);
				// 给每一个日子加上监听
				col.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ViewGroup parent = (ViewGroup) v.getParent();
						int row = 0, col = 0;

						// 获取列坐标
						for (int i = 0; i < parent.getChildCount(); i++) {
							if (v.equals(parent.getChildAt(i))) {
								col = i;
								break;
							}
						}
						// 获取行坐标
						ViewGroup pparent = (ViewGroup) parent.getParent();
						for (int i = 0; i < pparent.getChildCount(); i++) {
							if (parent.equals(pparent.getChildAt(i))) {
								row = i;
								break;
							}
						}
						if (onCalendarClickListener != null && !isOnScroll) {
							onCalendarClickListener.onCalendarClick(row, col,
									dates[row][col]);
						}
					}
				});
			}
		}
	}

	/**
	 * 填充日历(包含日期、标记、背景等)
	 */
	private void setCalendarDate() {
		// 根据日历的日子获取这一天是星期几
		int weekday = calendarday.getDay();
		// 每个月第一天
		int firstDay = 1;
		// 每个月中间号,根据循环会自动++
		int day = firstDay;
		// 每个月的最后一天
		int lastDay = getDateNum(calendarday.getYear(), calendarday.getMonth());
		// 下个月第一天
		int nextMonthDay = 1;
		int lastMonthDay = 1;
		// 填充每一个空格
		for (int i = 0; i < ROWS_TOTAL; i++) {
			for (int j = 0; j < COLS_TOTAL; j++) {
				// 这个月第一天不是礼拜天,则需要绘制上个月的剩余几天
				if (i == 0 && j == 0 && weekday != 0) {
					int year = 0;
					int month = 0;
					int lastMonthDays = 0;
					// 如果这个月是1月，上一个月就是去年的12月
					if (calendarday.getMonth() == 0) {
						year = calendarday.getYear() - 1;
						month = Calendar.DECEMBER;
					} else {
						year = calendarday.getYear();
						month = calendarday.getMonth() - 1;
					}
					// 上个月的最后一天是几号
					lastMonthDays = getDateNum(year, month);
					// 第一个格子展示的是几号
					int firstShowDay = lastMonthDays - weekday + 1;
					// 上月
					for (int k = 0; k < weekday; k++) {
						lastMonthDay = firstShowDay + k;
						RelativeLayout group = getDateView(0, k);
						group.setGravity(Gravity.CENTER);
						TextView view = null;
						if (group.getChildCount() > 0) {
							view = (TextView) group.getChildAt(0);
						} else {
							LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
									-1, -1);
							view = new TextView(getContext());
							view.setLayoutParams(params);
							view.setGravity(Gravity.CENTER);
							group.addView(view);
						}
						// 设置上个月的日期
						// view.setText(Integer.toString(lastMonthDay));
						view.setText("");
						dates[0][k] = format(new Date(year, month, lastMonthDay));
						if (hasMarked(dates[0][k])) {
							view.setTextColor(Color.parseColor("#000000"));
						} else {
							view.setTextColor(Color.parseColor("#BBBBBB"));
						}
						// 设置日期背景色
						if (dayBgColorMap.get(dates[0][k]) != null) {
							// view.setBackgroundResource(dayBgColorMap
							// .get(dates[i][j]));
						} else {
							view.setBackgroundColor(Color.TRANSPARENT);
						}
						// 设置标记
						setMarkerNull(group);
					}
					j = weekday - 1;
					// 这个月第一天是礼拜天，不用绘制上个月的日期，直接绘制这个月的日期
				} else {
					RelativeLayout group = getDateView(i, j);
					group.setGravity(Gravity.CENTER);
					TextView view = null;
					if (group.getChildCount() > 0) {
						view = (TextView) group.getChildAt(0);
					} else {
						LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
								-1, -1);
						view = new TextView(getContext());
						view.setLayoutParams(params);
						view.setGravity(Gravity.CENTER);
						group.addView(view);
					}

					// 本月
					if (day <= lastDay) {
						dates[i][j] = format(new Date(calendarday.getYear(),
								calendarday.getMonth(), day));
						view.setText(Integer.toString(day));
						// 当天
						if (thisday.getDate() == day
								&& thisday.getMonth() == calendarday.getMonth()
								&& thisday.getYear() == calendarday.getYear()) {
							view.setText("今");
							view.setTextColor(Color.parseColor("#000000"));
							view.setBackgroundResource(R.drawable.ring_calendar);
						} else {
							if (hasMarked(dates[i][j])) {
								view.setTextColor(Color.parseColor("#000000"));
							} else {
								view.setTextColor(Color.parseColor("#BBBBBB"));
							}
							view.setBackgroundColor(Color.TRANSPARENT);
						}
						// 上面首先设置了一下默认的"当天"背景色，当有特殊需求时，才给当日填充背景色
						// 设置日期背景色
						if (dayBgColorMap.get(dates[i][j]) != null) {
							view.setTextColor(Color.WHITE);
							view.setBackgroundResource(dayBgColorMap
									.get(dates[i][j]));
						}
						// 设置标记
						setMarker(group, i, j, dates[i][j]);
						day++;
						// 下个月
					} else {
						if (calendarday.getMonth() == Calendar.DECEMBER) {
							dates[i][j] = format(new Date(
									calendarday.getYear() + 1,
									Calendar.JANUARY, nextMonthDay));
						} else {
							dates[i][j] = format(new Date(
									calendarday.getYear(),
									calendarday.getMonth() + 1, nextMonthDay));
						}
						// 下个月的几天
						// view.setText(Integer.toString(nextMonthDay));
						view.setText("");
						if (hasMarked(dates[i][j])) {
							view.setTextColor(Color.parseColor("#000000"));
						} else {
							view.setTextColor(Color.parseColor("#BBBBBB"));
						}
						// 设置日期背景色
						if (dayBgColorMap.get(dates[i][j]) != null) {
							// view.setBackgroundResource(dayBgColorMap
							// .get(dates[i][j]));
						} else {
							view.setBackgroundColor(Color.TRANSPARENT);
						}
						// 设置标记
						setMarkerNull(group);
						nextMonthDay++;
					}
				}
			}
		}
		updateCalendarHeight(parent, params_parent);
	}

	/**
	 * onClick接口回调
	 */
	public interface OnCalendarClickListener {
		void onCalendarClick(int row, int col, String dateFormat);
	}

	/**
	 * ondateChange接口回调
	 */
	public interface OnCalendarDateChangedListener {
		void onCalendarDateChanged(int year, int month);
	}

	/**
	 * 根据具体的某年某月，展示一个日历
	 * 
	 * @param year
	 * @param month
	 */
	public void showCalendar(int year, int month) {
		calendarYear = year;
		calendarMonth = month - 1;
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		setCalendarDate();
	}

	/**
	 * 根据当前月，展示一个日历
	 * 
	 * @param year
	 * @param month
	 */
	public void showCalendar() {
		Date now = new Date();
		calendarYear = now.getYear() + 1900;
		calendarMonth = now.getMonth();
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		setCalendarDate();
	}

	/**
	 * 设置指定年月日历
	 * 
	 * @param calendarYear_new
	 *            年
	 * @param calendarMonth_new
	 *            月
	 */
	public synchronized void setYearAndMonth(int calendarYear_new,
			int calendarMonth_new) {
		if (calendarYear == calendarYear_new
				&& calendarMonth == calendarMonth_new - 1) {
			return;
		}
		// 改变日历上下顺序
		if (currentCalendar == firstCalendar) {
			currentCalendar = secondCalendar;
		} else {
			currentCalendar = firstCalendar;
		}
		// 指定日期在当前日期之前还是之后
		boolean isNext = calendarYear < calendarYear_new
				|| (calendarYear == calendarYear_new && calendarMonth < calendarMonth_new - 1);
		// 设置动画
		if (isNext) {// 之后
			setInAnimation(push_left_in);
			setOutAnimation(push_left_out);
		} else {// 之前
			setInAnimation(push_right_in);
			setOutAnimation(push_right_out);
		}
		// 改变日历日期
		calendarYear = calendarYear_new;
		calendarMonth = calendarMonth_new - 1;
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		// 填充日历
		setCalendarDate();
		// 下翻到下一月
		if (isNext) {
			showNext();
		} else {
			showPrevious();
		}
		// 回调
		if (onCalendarDateChangedListener != null) {
			onCalendarDateChangedListener.onCalendarDateChanged(calendarYear,
					calendarMonth + 1);
		}
	}

	/**
	 * 下一月日历
	 */
	public synchronized void nextMonth() {

		if (thisday.getMonth() == 0) {// 当前月为1月，滑动12月后不能继续滑动
			if (calendarMonth == Calendar.DECEMBER) {
				return;
			}
		} else {// 当前月为n月，滑动下一年n-1月后不能继续滑动
			if (calendarMonth == thisday.getMonth() - 1) {
				return;
			}
		}
		// 改变日历上下顺序
		if (currentCalendar == firstCalendar) {
			currentCalendar = secondCalendar;
		} else {
			currentCalendar = firstCalendar;
		}
		// 设置动画
		setInAnimation(push_left_in);
		setOutAnimation(push_left_out);
		// 改变日历日期
		if (calendarMonth == Calendar.DECEMBER) {
			calendarYear++;
			calendarMonth = Calendar.JANUARY;
		} else {
			calendarMonth++;
		}
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		// 填充日历
		setCalendarDate();
		// 下翻到下一月
		showNext();
		// 回调
		if (onCalendarDateChangedListener != null) {
			onCalendarDateChangedListener.onCalendarDateChanged(calendarYear,
					calendarMonth + 1);
		}
	}

	/**
	 * 上一月日历
	 */
	public synchronized void lastMonth() {

		if (thisday.getYear() == calendarYear - 1900
				&& thisday.getMonth() == calendarMonth) {
			return;
		}
		if (currentCalendar == firstCalendar) {
			currentCalendar = secondCalendar;
		} else {
			currentCalendar = firstCalendar;
		}
		setInAnimation(push_right_in);
		setOutAnimation(push_right_out);
		if (calendarMonth == Calendar.JANUARY) {
			calendarYear--;
			calendarMonth = Calendar.DECEMBER;
		} else {
			calendarMonth--;
		}
		calendarday = new Date(calendarYear - 1900, calendarMonth, 1);
		setCalendarDate();
		showPrevious();
		if (onCalendarDateChangedListener != null) {
			onCalendarDateChangedListener.onCalendarDateChanged(calendarYear,
					calendarMonth + 1);
		}
	}

	/**
	 * 获取日历当前年份
	 */
	public int getCalendarYear() {
		return calendarday.getYear() + 1900;
	}

	/**
	 * 获取日历当前月份
	 */
	public int getCalendarMonth() {
		return calendarday.getMonth() + 1;
	}

	/**
	 * 在日历上做一个标记
	 * 
	 * @param date
	 *            日期
	 * @param id
	 *            bitmap res id
	 */
	public void addMark(Date date, int id) {
		addMark(format(date), id);
	}

	/**
	 * 在日历上做一个标记
	 * 
	 * @param date
	 *            日期
	 * @param id
	 *            bitmap res id
	 */
	public void addMark(String date, int id) {
		marksMap.put(date, id);
		setCalendarDate();
	}

	/**
	 * 在日历上做一个标记
	 * 
	 * @param date
	 *            日期
	 * @param id
	 *            bitmap res id
	 */
	public void addMarkOnly(String date, int id) {
		marksMap.put(date, id);
	}

	/**
	 * 刷新日历
	 */
	public void notifyDataSetChanged() {
		setCalendarDate();
	}

	/**
	 * 在日历上做一组标记
	 * 
	 * @param date
	 *            日期
	 * @param id
	 *            bitmap res id
	 */
	public void addMarks(Date[] date, int id) {
		for (int i = 0; i < date.length; i++) {
			marksMap.put(format(date[i]), id);
		}
		setCalendarDate();
	}

	/**
	 * 在日历上做一组标记
	 * 
	 * @param date
	 *            日期
	 * @param id
	 *            bitmap res id
	 */
	public void addMarks(List<String> date, int id) {
		for (int i = 0; i < date.size(); i++) {
			marksMap.put(date.get(i), id);
		}
		setCalendarDate();
	}

	/**
	 * 移除日历上的标记
	 */
	public void removeMark(Date date) {
		removeMark(format(date));
	}

	/**
	 * 移除日历上的标记
	 */
	public void removeMark(String date) {
		marksMap.remove(date);
		setCalendarDate();
	}

	/**
	 * 移除日历上的所有标记
	 */
	public void removeAllMarks() {
		marksMap.clear();
		setCalendarDate();
	}

	/**
	 * 设置日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void setCalendarDayBgColor(Date date, int color) {
		setCalendarDayBgColor(format(date), color);
	}

	/**
	 * 设置日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void setCalendarDayBgColor(String date, int color) {
		dayBgColorMap.put(date, color);
		setCalendarDate();
	}

	/**
	 * 设置日历一组日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void setCalendarDaysBgColor(List<String> date, int color) {
		for (int i = 0; i < date.size(); i++) {
			dayBgColorMap.put(date.get(i), color);
		}
		setCalendarDate();
	}

	/**
	 * 设置日历一组日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void setCalendarDayBgColor(String[] date, int color) {
		for (int i = 0; i < date.length; i++) {
			dayBgColorMap.put(date[i], color);
		}
		setCalendarDate();
	}

	/**
	 * 移除日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void removeCalendarDayBgColor(Date date) {
		removeCalendarDayBgColor(format(date));
	}

	/**
	 * 移除日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void removeCalendarDayBgColor(String date) {
		dayBgColorMap.remove(date);
		setCalendarDate();
	}

	/**
	 * 移除日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void removeAllBgColor() {
		dayBgColorMap.clear();
		setCalendarDate();
	}

	/**
	 * 移除日历具体某个日期的背景色
	 * 
	 * @param date
	 * @param color
	 */
	public void removeAllBgColorOnly() {
		dayBgColorMap.clear();
	}

	/**
	 * 根据行列号获得包装每一个日子的LinearLayout
	 * 
	 * @param row
	 * @param col
	 * @return
	 */
	public String getDate(int row, int col) {
		return dates[row][col];
	}

	/**
	 * 某天是否被标记了
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public boolean hasMarked(String date) {
		return marksMap.get(date) == null ? false : true;
	}

	public boolean hasSelected(String date) {
		return dayBgColorMap.get(date) == null ? false : true;
	}

	/**
	 * 清除所有标记以及背景
	 */
	public void clearAll() {
		marksMap.clear();
		dayBgColorMap.clear();
	}

	/***********************************************
	 * 
	 * private methods
	 * 
	 **********************************************/
	// 设置标记
	private void setMarker(RelativeLayout group, int i, int j, String date) {
		int childCount = group.getChildCount();
		if (marksMap.get(dates[i][j]) != null) {
			boolean isSelect = hasSelected(date);
			if (childCount < 2) {
				// 圆点容器
				LinearLayout linear_point = new LinearLayout(getContext());
				RelativeLayout.LayoutParams params_linear_pointLayoutParams = new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT);
				params_linear_pointLayoutParams
						.addRule(RelativeLayout.CENTER_HORIZONTAL);
				params_linear_pointLayoutParams
						.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				params_linear_pointLayoutParams
						.setMargins(0, 0, 0, (int) (SystemUtils
								.getScreenRealWidth(getContext()) / 7.0 / 6.0));
				linear_point.setLayoutParams(params_linear_pointLayoutParams);
				linear_point.setOrientation(LinearLayout.HORIZONTAL);

				if (marksMap.get(dates[i][j]) == POINT_RED_BLUE) {
					// 添加红色圆点
					ImageView iv_point_red = new ImageView(getContext());
					iv_point_red.setLayoutParams(new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar_select);
					} else {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar);
					}
					linear_point.addView(iv_point_red);
					// 添加蓝色圆点
					ImageView iv_point_blue = new ImageView(getContext());
					LinearLayout.LayoutParams params_blue = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT);
					params_blue.setMargins(SystemUtils.dip2px(getContext(), 3),
							0, 0, 0);
					iv_point_blue.setLayoutParams(params_blue);
					if (isSelect) {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar_select);
					} else {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar);
					}
					linear_point.addView(iv_point_blue);
				} else if (marksMap.get(dates[i][j]) == POINT_RED) {
					// 添加红色圆点
					ImageView iv_point_red = new ImageView(getContext());
					iv_point_red.setLayoutParams(new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar_select);
					} else {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar);
					}
					linear_point.addView(iv_point_red);
				} else if (marksMap.get(dates[i][j]) == POINT_BLUE) {
					// 添加蓝色圆点
					ImageView iv_point_blue = new ImageView(getContext());
					iv_point_blue
							.setLayoutParams(new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.WRAP_CONTENT,
									LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar_select);
					} else {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar);
					}
					linear_point.addView(iv_point_blue);
				}
				// 将圆点容器添加到group中
				group.addView(linear_point);
			} else if (childCount == 2) {
				LinearLayout linear = (LinearLayout) group.getChildAt(1);
				linear.removeAllViews();
				if (marksMap.get(dates[i][j]) == POINT_RED_BLUE) {
					// 添加红色圆点
					ImageView iv_point_red = new ImageView(getContext());
					iv_point_red.setLayoutParams(new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar_select);
					} else {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar);
					}
					linear.addView(iv_point_red);
					// 添加蓝色圆点
					ImageView iv_point_blue = new ImageView(getContext());
					LinearLayout.LayoutParams params_blue = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT);
					params_blue.setMargins(SystemUtils.dip2px(getContext(), 3),
							0, 0, 0);
					iv_point_blue.setLayoutParams(params_blue);
					if (isSelect) {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar_select);
					} else {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar);
					}
					linear.addView(iv_point_blue);
				} else if (marksMap.get(dates[i][j]) == POINT_RED) {
					// 添加红色圆点
					ImageView iv_point_red = new ImageView(getContext());
					iv_point_red.setLayoutParams(new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.WRAP_CONTENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar_select);
					} else {
						iv_point_red
								.setImageResource(R.drawable.point_red_calendar);
					}
					linear.addView(iv_point_red);
				} else if (marksMap.get(dates[i][j]) == POINT_BLUE) {
					// 添加蓝色圆点
					ImageView iv_point_blue = new ImageView(getContext());
					iv_point_blue
							.setLayoutParams(new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.WRAP_CONTENT,
									LinearLayout.LayoutParams.WRAP_CONTENT));
					if (isSelect) {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar_select);
					} else {
						iv_point_blue
								.setImageResource(R.drawable.point_blue_calendar);
					}
					linear.addView(iv_point_blue);
				}
			}
		} else {
			if (childCount > 1) {
				group.removeView(group.getChildAt(1));
			}
		}
	}

	/**
	 * 上下月预览天数不设置标记
	 * 
	 * @param group
	 */
	private void setMarkerNull(RelativeLayout group) {
		int childCount = group.getChildCount();
		if (childCount > 1) {
			group.removeView(group.getChildAt(1));
		}

	}

	/**
	 * 计算某年某月有多少天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDateNum(int year, int month) {
		Calendar time = Calendar.getInstance();
		time.clear();
		time.set(Calendar.YEAR, year + 1900);
		time.set(Calendar.MONTH, month);
		return time.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 根据行列号获得包装每一个日子的RelativeLayout
	 * 
	 * @param row
	 * @param col
	 * @return
	 */
	private RelativeLayout getDateView(int row, int col) {
		return (RelativeLayout) ((LinearLayout) ((LinearLayout) currentCalendar
				.getChildAt(1)).getChildAt(row)).getChildAt(col);
	}

	/**
	 * 根据行列号获得包装每一个日子的RelativeLayout所属的父容器LinearLayout
	 * 
	 * @param row
	 * @param col
	 * @return
	 */
	private LinearLayout getDateViewParent(int row) {
		return (LinearLayout) ((LinearLayout) currentCalendar.getChildAt(1))
				.getChildAt(row);
	}

	/**
	 * 将Date转化成字符串->2013-3-3
	 */
	public static String format(Date d) {
		return addZero(d.getYear() + 1900, 4) + "-"
				+ addZero(d.getMonth() + 1, 2) + "-" + addZero(d.getDate(), 2);
	}

	// 2或4
	public static String addZero(int i, int count) {
		if (count == 2) {
			if (i < 10) {
				return "0" + i;
			}
		} else if (count == 4) {
			if (i < 10) {
				return "000" + i;
			} else if (i < 100 && i > 10) {
				return "00" + i;
			} else if (i < 1000 && i > 100) {
				return "0" + i;
			}
		}
		return "" + i;
	}

	private float dertaY = 0;
	private float lastY = 0;
	private float dertaX = 0;
	private float lastX = 0;

	/***********************************************
	 * 
	 * Override methods
	 * 
	 **********************************************/
	public boolean dispatchTouchEvent(MotionEvent ev) {
		/*
		 * 避免父控件拦截touch事件
		 */
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			dertaY = 0;
			lastY = ev.getY();
			dertaX = 0;
			lastX = ev.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			dertaY = ev.getY() - lastY;
			dertaX = ev.getX() - lastX;
			lastX = ev.getX();
			lastY = ev.getY();
			break;
		default:
			break;
		}

		if (Math.abs(dertaX) > Math.abs(dertaY)) {
			ViewParent parent = getParent();
			while (parent != null) {
				parent.requestDisallowInterceptTouchEvent(true);
				parent = parent.getParent();
			}
		}

		/*
		 * 添加手势
		 */
		if (gd != null) {
			if (gd.onTouchEvent(ev))
				return true;
		}
		return super.dispatchTouchEvent(ev);
	}

	public boolean onTouchEvent(MotionEvent event) {
		return this.gd.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent e) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
	}

	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	public void onLongPress(MotionEvent e) {
	}

	private Timer mTimer;
	private TimerTask mTask;

	/**
	 * 取消timer和timertask
	 */
	public void cancelTimerAndTask() {
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		if (mTask != null) {
			mTask.cancel();
			mTask = null;
		}
	}

	/**
	 * 此处是为了防止滑动过程中日期可点击
	 */
	private void updateIsOnScroll() {
		isOnScroll = true;
		cancelTimerAndTask();
		mTimer = new Timer();
		mTask = new TimerTask() {

			@Override
			public void run() {
				if (isOnScroll) {
					isOnScroll = false;
				}
			}
		};
		mTimer.schedule(mTask, 400);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// 向左/上滑动
		if (e1.getX() - e2.getX() > 20) {
			nextMonth();
			updateIsOnScroll();
		}
		// 向右/下滑动
		else if (e1.getX() - e2.getX() < -20) {
			lastMonth();
			updateIsOnScroll();
		}
		return false;
	}

	/***********************************************
	 * 
	 * get/set methods
	 * 
	 **********************************************/

	public OnCalendarClickListener getOnCalendarClickListener() {
		return onCalendarClickListener;
	}

	public void setOnCalendarClickListener(
			OnCalendarClickListener onCalendarClickListener) {
		this.onCalendarClickListener = onCalendarClickListener;
	}

	public OnCalendarDateChangedListener getOnCalendarDateChangedListener() {
		return onCalendarDateChangedListener;
	}

	public void setOnCalendarDateChangedListener(
			OnCalendarDateChangedListener onCalendarDateChangedListener) {
		this.onCalendarDateChangedListener = onCalendarDateChangedListener;
	}

	public Date getThisday() {
		return thisday;
	}

	public void setThisday(Date thisday) {
		this.thisday = thisday;
	}

	public Map<String, Integer> getDayBgColorMap() {
		return dayBgColorMap;
	}

	public void setDayBgColorMap(Map<String, Integer> dayBgColorMap) {
		this.dayBgColorMap = dayBgColorMap;
	}

	/**
	 * KCalendar的父View
	 */
	private View parent;
	/**
	 * KCalendar的父View的参数
	 */
	private android.view.ViewGroup.LayoutParams params_parent;

	/**
	 * 设置KCalendar的父View和父View的参数
	 * 
	 * @param parent
	 * @param params_parent
	 */
	public void setParentAndParams(View parent,
			android.view.ViewGroup.LayoutParams params_parent) {
		this.parent = parent;
		this.params_parent = params_parent;
	}

	/**
	 * 根据显示的行数重新设置KCalendar的父View的高度
	 */
	public void updateCalendarHeight(View parent,
			android.view.ViewGroup.LayoutParams params) {
		try {
			if (parent != null && params != null) {
				// 日历显示行数,可能为5，6，7
				int totalRow;
				// 日历第六行
				RelativeLayout group_six = getDateView(4, 0);
				// 日历第七行
				RelativeLayout group_seven = getDateView(5, 0);
				if (group_six.getChildCount() > 0) {
					TextView view_six = (TextView) group_six.getChildAt(0);
					if (!TextUtils
							.isEmpty(view_six.getText().toString().trim())) {
						totalRow = 6;
						if (group_seven.getChildCount() > 0) {
							TextView view_seven = (TextView) group_seven
									.getChildAt(0);
							if (!TextUtils.isEmpty(view_seven.getText()
									.toString().trim())) {
								totalRow = 7;
							} else {
								totalRow = 6;
							}
						} else {
							totalRow = 6;
						}
					} else {
						totalRow = 5;
					}
				} else {
					totalRow = 5;
				}

				int screenWidth = SystemUtils.getScreenWidth(getContext());
				params.width = screenWidth;
				params.height = (int) (screenWidth * 6.7 / 7)
						- (screenWidth / 7 * (7 - totalRow));
				parent.setLayoutParams(params);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}